package cn.eveo.demo;


import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.eveo.trypagerindicator.TryPagerIndicator;


/**
 * A simple {@link Fragment} subclass.
 */
public class IconFragment extends Fragment {
    private String[] titles = {"flyme", "xiaomi", "huawei", "sungsong", "iphone", "demo", "cat", "bad"};
    private TryPagerIndicator tabLayout;
    private ViewPager viewPager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_icon, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        tabLayout = (TryPagerIndicator) view.findViewById(R.id.trypagerindicator);
        viewPager = (ViewPager) view.findViewById(R.id.viewpager);

        viewPager.setAdapter(new TitleAdapter());
        tabLayout.setViewPager(viewPager);
    }

    private class TitleAdapter extends PagerAdapter implements TryPagerIndicator.IconProvider {

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            TextView textView = new TextView(getContext());
            textView.setGravity(Gravity.CENTER);
            textView.setText(titles[position]);
            container.addView(textView);
            return textView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Drawable getIcon() {
            Drawable drawable = getResources().getDrawable(R.drawable.icon);
            return drawable;
        }
    }

}
